package cn.enaium.message.controller;

import cn.enaium.message.specific.Logon;
import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;

@RestController
public class MailController {
    String code= "";
    //发送邮件
    @GetMapping(value = "/sentmail")
    public String sentmail( @NonNull String email){
        String vertifyCode=createVertifyCode();
        Properties prop = new Properties();
        // 开启debug调试，以便在控制台查看
        prop.setProperty("mail.debug", "true");
        // 设置邮件服务器主机名
        prop.setProperty("mail.host", "smtp.qq.com");
        // 发送服务器需要身份验证
        prop.setProperty("mail.smtp.auth", "true");
        // 发送邮件协议名称
        prop.setProperty("mail.transport.protocol", "smtp");
        // 开启SSL加密，否则会失败
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        assert sf != null;
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        // 创建session
        Session session = Session.getInstance(prop);

        try {
            Transport ts = session.getTransport();
            try {
                // 连接邮件服务器：邮箱类型，帐号，POP3/SMTP协议授权码 .163使用：smtp.163.com
                ts.connect("smtp.qq.com", "3394737244", "tyeckrmfunrlchdh");
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            Message message = null;
            try {
                // 创建邮件
                message = createSimpleMail(session,vertifyCode,email);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                //修改邮件接收方为传入的参数(邮件地址)
                assert message != null;
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            // 发送邮件
            try {
                ts.sendMessage(message, message.getAllRecipients());
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            try {
                ts.close();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }
        //保留验证码等待对比
        return vertifyCode;
    }
    //  获取6位随机验证码
    public String createVertifyCode(){
        String[] letters = new String[] {
                "q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
                "A","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M",
                "0","1","2","3","4","5","6","7","8","9"};
        String stringBuilder ="";
        for (int i = 0; i < 6; i++) {
            stringBuilder = stringBuilder + letters[(int)Math.floor(Math.random()*letters.length)];
        }
        return stringBuilder;
    }
    public static MimeMessage createSimpleMail(Session session, String str,String mail) throws Exception {
        // 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        // 指明邮件的发件人
        message.setFrom(new InternetAddress("confessionwall512@qq.com"));
        // 指明邮件的收件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(mail));
        // 邮件的标题
        message.setSubject("树洞验证码");
        // 邮件的文本内容
        message.setContent("您正在使用邮箱验证,重置验证码为(一分钟有效):"+str+",请勿回复此邮箱", "text/html;charset=UTF-8");
        // 返回创建好的邮件对象
        return message;
    }
    //判断注册
    @GetMapping(value = "/logon")
    public boolean Register(String name,String password,String email){
        Logon logon = new Logon();
        return logon.RegisterUser(name, password, email);
    }


}
